<?php
namespace app\admin\controller;

use app\admin\model\AdminGroup as AdminGroupModel;
use app\admin\model\Admin as AdminModel;
use app\admin\model\AdminJob as AdminJobModel;
use think\Db;
use think\Request;

class AdminManage extends Base
{
    public function index()
    {
        $model = new AdminModel;
        $aGroupModel = new AdminGroupModel;
        $aJobModel = new AdminJobModel;

        //渲染页面模板
        $model->field('a.id,a.admin_group_id,a.username,a.mobile,a.email,a.created_at,a.updated_at,
            g.name as gname, g.power, g.illustration as gillustration')/*j.name as jname,j.illustration as jillustration*/
            ->alias('a')
            ->join('admin_group g','a.admin_group_id = g.id ','left')
           // ->join('admin_job j','g.admin_job_id = j.id','left')
            //->where('g.admin_job_id','j.id')
            ->order('a.id','desc');
        //->select();
        // ->paginate(5);

        //搜索名称
        $phname = $this->request->param("phname");

        if(!empty($phname)){
            $model->where('a.username|a.mobile','like', '%'.$phname.'%');
        }
        $result = $model->paginate(10,false,[
            'query'=>['phname'=>$phname]
        ]);
        $items = $result->items();

        //循环数组给数组加上属性
        foreach ($items as $k=>$v){
           $arr =  \think\Db::query('select name from '. $this->dbTablePrefix .'menu where id in ( select bb.sss from
            (SELECT substring_index(substring_index(aa.menu_id,",", b.help_topic_id + 1), ",", -1) as sss
      FROM (select menu_id from '. $this->dbTablePrefix .'admin_group where id=' . $v["admin_group_id"] . ') as aa
      join mysql.help_topic b ON b.help_topic_id < (LENGTH(aa.menu_id) - LENGTH(REPLACE(aa.menu_id, ",", "")) + 1)
    ) as bb)');
          $items[$k]["jname"] = implode(',',array_column($arr,'name'));
        }
        //渲染用户组
        $aGroup = $aGroupModel->field('id,name')->where('flag','0')->select();
        //渲染代理商
        $aJob = $aJobModel->field('id,name')->select();

        //计算总共记录条数
        $count = $result->total();
        //分页
        $page = $result->render();

        //接收ajax传递的值
        $request = request();
        $ispost = $request->isPost();
        if($ispost){
            $id = $_POST["viewId"];
            $view = $model->getAll()->where("id",$id)->find();
            return json([
                'code' => 200,
                'view'=>$view,
            ]);
        }


        $this->assign('currentPage',$result->currentPage());
        $this->assign('lastPage',$result->lastPage());
        $this->assign('page',$page);
        $this->assign('aGroup',$aGroup);
        $this->assign('aJob',$aJob);
        $this->assign('admin',$result);
        $this->assign('count',$count);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }

    //新增数据
    public function create()
    {
        $model = new AdminModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();
        if($ispost){
            $formdata = $this->request->post();
            $username = Db::query("select id,username from ". $this->dbTablePrefix ."admin where `username`='".$formdata['username']."' and deleted_at is NULL ");
            if(!empty($username)){
                return json([
                    'code' => 4000,
                    'message'=>"用户名已存在"
                ]);
            }
            if(empty($formdata["password"])){
                return json([
                    'code' => 4001,
                    'message'=>"密码不能为空"
                ]);
            }
            $formdata["password"] =  md5($formdata["password"] + "zhinengyunjing");
            $result = $model->allowField(true)->save($formdata);
            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //修改数据处理
    public function edit($id)
    {
        $model = new AdminModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();
        if($ispost){
            $formdata = $this->request->post();
            $formdata["password"] = md5($formdata["password"] + "zhinengyunjing");
            $result = $model->allowField(true)->save($formdata,['id'=>$id]);
            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //删除单条记录
    public function delete($id)
    {
        $result = AdminModel::update(['deleted_at'=>date('Y-m-d H:i:s')],['id'=>$id]);//软删除
        //AlltimeModel::destroy($id,true);//真实删除
        if($result){
            return json([
                'code' => 200,
            ]);
        }

    }

    //批量删除
    public function deleteArr()
    {
        $ispost = $this->request->isPost();
        if ($ispost) {
            $idArr = $this->request->post();

            $result = AdminModel::where('id','exp','in('.implode(',',$idArr['id']).')')->update(['deleted_at'=>date('Y-m-d H:i:s')]);//软删除

            //AlltimeModel::destroy($id,true);//真实删除
            if($result){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //新增权限
    public function createJob()
    {
        $model = new AdminJobModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();
        if($ispost){
            $formdata = $this->request->post();
            $name = Db::query("select id,name from ". $this->dbTablePrefix ."admin_job where `name`='".$formdata['name']."' and deleted_at is NULL ");

            if(!empty($name)){
                return json([
                    'code' => 4000,
                    'message'=>"权限已存在"
                ]);
            }
            $result = $model->allowField(true)->save($formdata);

            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //新增角色
    public function createGroup()
    {
        $model = new AdminJobModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();
        if($ispost){
            $formdata = $this->request->post();
            $name = Db::query("select id,name from ". $this->dbTablePrefix ."admin_job where `name`='".$formdata['name']."' and deleted_at is NULL ");

            if(!empty($name)){
                return json([
                    'code' => 4000,
                    'message'=>"权限已存在"
                ]);
            }
            $result = $model->allowField(true)->save($formdata);

            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }
}